<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>工作人员</el-breadcrumb-item>
      <el-breadcrumb-item>工作人员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="5">
          <el-input placeholder="请输入姓名" v-model="queryInfo.query" clearable @clear="getWorkers">
            <el-button slot="append" icon="el-icon-search" @click="getWorkers"></el-button>
          </el-input>
        </el-col>
        <el-col :span="5" style="padding-left:20px">
          <el-select v-model="queryInfo.community" placeholder="全部" @change="getWorkers">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value"></el-option>
            <el-option label="全部" value=""></el-option>
          </el-select>
        </el-col>
        <el-col :span="4" style="padding-left:20px">
          <el-button type="primary" @click="ShowAddWorkerdialogVisible" v-if="this.userName==='admin'">添加工作人员</el-button>
        </el-col>
      </el-row>
      <el-table :data="worklist">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="姓名" prop="workName"></el-table-column>
        <el-table-column label="工号" prop="workId"></el-table-column>
        <el-table-column label="电话" prop="workPhone"></el-table-column>
        <el-table-column label="所属社区" prop="community"></el-table-column>
        <el-table-column label="工作区域" prop="address"></el-table-column>
        <el-table-column label="职位" prop="jobs"></el-table-column>
        <el-table-column label="状态" >
          <template slot-scope="{row}">
            <el-tag type="success" v-if="row.workState !==1">工作中</el-tag>
            <el-tag type="info" v-else>休息中</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" v-if="this.userName==='admin'">
          <template slot-scope="{row}">
            <el-button type="primary" @click="showEdit(row.workId)" size="mini">修改</el-button>
            <el-button slot="reference" type="warning" size="mini" @click="removeById(row.workId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pageNum" :page-sizes="[5, 10, 30, 50]" :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" style="text-align:center" background>
      </el-pagination>
    </el-card>

    <!-- 添加工作人员对话框 -->
    <el-dialog title="添加" :visible.sync="addWorkerdialogVisible" width="50%" @close="resetAddForm">
      <el-form :model="WorkerForm" :rules="rules" ref="WorkerFormRef" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="WorkerForm.name"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="WorkerForm.phone" type="number"></el-input>
        </el-form-item>
        <el-form-item label="社区选择" prop="community">
          <el-select v-model="WorkerForm.community" placeholder="请选择">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作区域" prop="address">
          <el-input v-model="WorkerForm.address"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="jobs">
          <el-input v-model="WorkerForm.jobs"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input v-model="WorkerForm.pass" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input v-model="WorkerForm.checkPass" type="password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetAddForm">取 消</el-button>
        <el-button type="primary" @click="addWorker">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改工作人员对话框 -->
    <el-dialog title="修改" :visible.sync="editWorkerdialogVisible" width="50%" @close="resetEditForm">
      <el-form :model="editForm" :rules="rules" ref="editFormRef" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.workName" disabled></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.workPhone"></el-input>
        </el-form-item>
        <el-form-item label="工作社区" prop="community">
          <el-select v-model="editForm.community" :placeholder="editForm.community">
            <el-option v-for="item in cityData" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作区域" prop="address">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="jobs">
          <el-input v-model="editForm.jobs"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editWorkerdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editWorker">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import cityData from '../../assets/js/cityData.js'
export default {
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.WorkerForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 5,
        community: '',
        myidentity: window.sessionStorage.getItem('identity')
      },
      userName: window.sessionStorage.getItem('userName'),
      worklist: [],
      total: 0,
      addWorkerdialogVisible: false,
      editWorkerdialogVisible: false,
      rules: {
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        community: [{ required: true, message: '请输入工作社区', trigger: 'blur' }],
        address: [{ required: true, message: '请输入工作地址', trigger: 'blur' }],
        jobs: [{ required: true, message: '请输入职位', trigger: 'blur' }],
        pass: [{ min: 5, max: 14, message: '长度在 3 到 5 个字符', trigger: 'blur' },
          { required: true, message: '请输入密码', trigger: 'blur' }]
      },
      WorkerForm: {
        name: '',
        phone: '',
        address: '',
        jobs: '',
        pass: '',
        checkPass: '',
        community: ''
      },
      editForm: {},
      cityData
    }
  },
  created () {
    this.getWorkers()
  },
  methods: {
    // 获取工作人员列表
    async getWorkers () {
      const { data: res } = await this.$http.get('work/getWork', { params: this.queryInfo })
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('请求员工列表数据失败')
      }
      this.$message.success('请求员工列表数据成功')
      this.worklist = res.data.records
      this.total = res.data.total
      console.log(this.worklist)
    },
    // 分页器
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getWorkers()
    },
    handleCurrentChange (newSize) {
      this.queryInfo.pageNum = newSize
      this.getWorkers()
    },
    // 展示添加工作人员
    ShowAddWorkerdialogVisible () {
      this.addWorkerdialogVisible = true
    },
    // 添加工作人员
    addWorker () {
      this.$refs.WorkerFormRef.validate(async valid => {
        if (!valid) return this.$message.error('数据校验失败')
        const { data: res } = await this.$http.post('work/addWorker', {
          workName: this.WorkerForm.name,
          workPhone: this.WorkerForm.phone,
          workPassword: this.WorkerForm.checkPass,
          address: this.WorkerForm.address,
          jobs: this.WorkerForm.jobs,
          community: this.WorkerForm.community
        })
        console.log(res)
        if (res.meta.status !== 200) {
          this.addWorkerdialogVisible = false
          return this.$message.error('添加失败')
        }
        // console.log(res)
        this.$message.success('成功')
        this.addWorkerdialogVisible = false
        this.getWorkers()
      })
    },
    // 重置表单
    resetAddForm () {
      this.$refs.WorkerFormRef.resetFields()
    },
    resetEditForm () {
      this.$refs.editFormRef.resetFields()
    },
    // 展示修改
    async showEdit (id) {
      this.editWorkerdialogVisible = true
      const { data: res } = await this.$http.get(`work/getWorkerById/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('修改信息失败')
      }
      this.editForm = res.data
    },
    // 修改
    async editWorker () {
      const { data: res } = await this.$http.put(`work/editWorkerById/${this.editForm.workId}`, this.editForm)
      if (res.meta.status !== 200) {
        return this.$message.error('修改失败')
      }
      // console.log(res)
      this.$message.success('成功')
      this.editWorkerdialogVisible = false
      this.getWorkers()
    },
    // 删除
    async removeById (id) {
      const confirm = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => {
        console.log(err)
      })
      if (confirm !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`work/deleteWorkerById/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除用户失败')
      }
      this.$message.success('删除用户成功')
      this.getWorkers()
    }
  },
  computed: {
    ...mapGetters(['identity'])
  }
}
</script>

<style lang="less" scoped>
</style>
